package com.caimiao.miao.page.master.system.bluetooth;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.caimiao.miao.R;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.library.bluetooth.BTDevice;
import com.caimiao.miao.library.bluetooth.IBluetoothCode;

public class BluetoothActivity extends BaseActivity<IBluetoothContact.Presenter> implements IBluetoothContact.View, View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView mListView;
    private EditText mEtWrite;
    private TextView mTvRead;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_bluetooth;
    }

    @Override
    protected void initUI() {
        setTitle("蓝牙设备");
        mListView = findViewById(R.id.bluetoothLvDevices);
        mEtWrite = findViewById(R.id.bluetoothEtWrite);
        mTvRead = findViewById(R.id.bluetoothTvRead);
    }

    @Override
    protected void initLogic() {
        findViewById(R.id.bluetoothBtnSend).setOnClickListener(this::onClick);
        findViewById(R.id.bluetoothBtnClear).setOnClickListener(this::onClick);
        findViewById(R.id.bluetoothBtnSearch).setOnClickListener(this::onClick);
        mListView.setOnItemClickListener(this::onItemClick);
        new BluetoothPresenter(this);
        mEtWrite.setText("FF 55 F0 01 00");
        /*FF 55 F0 01 00 26 F0
          FF 55 A0 01 00 22 B0
          FF 55 A1 01 00 A2 A7
          FF 55 B0 01 00 A3 F3
          FF 55 D0 01 00 A4 73
          FF 55 A2 01 64 23 C0*/
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        mListView.setAdapter(adapter);
    }

    @Override
    public void setRead(String value) {
        StringBuilder sb = new StringBuilder(mTvRead.getText().toString());
        sb.append(value);
        mTvRead.setText(sb.toString());
    }

    @Override
    public String getWrite() {
        return mEtWrite.getText().toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bluetoothBtnSend:
                mPresenter.send(getWrite());
                break;
            case R.id.bluetoothBtnSearch:
//                mPresenter.startDiscovery(this);
                break;
            case R.id.bluetoothBtnClear:
                mTvRead.setText("");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IBluetoothCode.REQUEST_ENABLE_BT) {
            if (requestCode == RESULT_CANCELED) {

            } else if (resultCode == RESULT_OK) {
                mPresenter.findBluetoothDevice(this);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BTDevice device = (BTDevice) parent.getItemAtPosition(position);
        mPresenter.connect(device);
    }
}
